package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ProductZhang;
import com.ruoyi.system.service.IProductZhangService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProductHistory;
import com.ruoyi.system.service.IProductHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * zhangController
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/system/history")
public class ProductHistoryController extends BaseController
{
    @Autowired
    private IProductHistoryService productHistoryService;

    @Autowired
    private IProductZhangService productZhangService;

    /**
     * 查询zhang列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductHistory productHistory)
    {
        startPage();
        List<ProductHistory> list = productHistoryService.selectProductHistoryList(productHistory);
        return getDataTable(list);
    }

    /**
     * 导出zhang列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:export')")
    @Log(title = "zhang", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductHistory productHistory)
    {
        List<ProductHistory> list = productHistoryService.selectProductHistoryList(productHistory);
        ExcelUtil<ProductHistory> util = new ExcelUtil<ProductHistory>(ProductHistory.class);
        util.exportExcel(response, list, "zhang数据");
    }

//    /**
//     * 获取zhang详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:history:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(productHistoryService.selectProductHistoryById(id));
//    }

    /**
     * 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        ProductHistory productHistory = productHistoryService.selectProductHistoryById(String.valueOf(id));
        //获得类别
        String[] split = productHistory.getListClass().split(",");
        //获得数量
        String[] split1 = productHistory.getNumber().split(",");
        List<ProductZhang> list = new ArrayList<>();
        for (int i=0;i<split.length;i++){
            ProductZhang productZhang = productZhangService.selectProductZhangById(split[i]);
            productHistory.setNumber(split1[i]);
            list.add(productZhang);
        }
        //返回list
        return success(list);
    }

    /**
     * 新增zhang
     */
    @PreAuthorize("@ss.hasPermi('system:history:add')")
    @Log(title = "zhang", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductHistory productHistory)
    {
        return toAjax(productHistoryService.insertProductHistory(productHistory));
    }

    /**
     * 修改zhang
     */
    @PreAuthorize("@ss.hasPermi('system:history:edit')")
    @Log(title = "zhang", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductHistory productHistory)
    {
        return toAjax(productHistoryService.updateProductHistory(productHistory));
    }

    /**
     * 删除zhang
     */
    @PreAuthorize("@ss.hasPermi('system:history:remove')")
    @Log(title = "zhang", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productHistoryService.deleteProductHistoryByIds(ids));
    }
}
